<?php
/**
 * Created by PhpStorm.
 * User: xionghao
 * Date: 2017/10/18
 * Time: 11:27
 */

namespace app\user\controller;


use app\meeting\model\MeetModel;
use app\user\model\MeetingLogModel;
use app\user\model\UserOrderModel;
use cmf\controller\UserBaseController;

class MeetController extends UserBaseController
{
    public function index()
    {
        $user = cmf_get_current_user();
        $this->assign('user', $user);
        $this->assign('more', json_decode($user['more'], true));
        $meetModel = new MeetModel();
        $list = $meetModel->with('provinceText')->order('start_time', 'desc')->paginate(10);
        $this->assign('list', $list->items());
//        var_dump($list->items());
//        exit;
        $this->assign('count', $list->total());
        if ($this->request->isAjax()) {
            return $this->fetch('ajax_list');
        }
        return $this->fetch();
    }

    public function log(){
        $logModel = new MeetingLogModel();
        $user = cmf_get_current_user();
        $this->assign('user', $user);
        $this->assign('more', json_decode($user['more'], true));
        $list = $logModel->with('meeting')->order('create_time', 'desc')->paginate(10);
        $this->assign('list', $list->items());
//        var_dump($list->items());
//        exit;
        $this->assign('count', $list->total());
        if ($this->request->isAjax()) {
            return $this->fetch('ajax_list_log');
        }
        return $this->fetch();

    }

    public function join()
    {
        $mid = $this->request->param('mid');
        $meeting = MeetModel::where("id", $mid)->where('register_end_time', '>', time())->find();
        if (!$meeting) {
            return $this->error('不在参会报名时间，请在报名时间内进行报名');

        }
        $user = cmf_get_current_user();
        if (!$user) {
            return $this->error('参数错误');
        }
        $meetingLog = MeetingLogModel::where('uid', $user['id'])->where('mid', $mid)->find();
        if ($meetingLog) {
            return $this->error('您已参加会议,无需继续报名参会');
        }

        $orderModel = new UserOrderModel();

        $order_info = [
            'order_no' => date('YmdHm') . random_int(1000, 9999),
            'price' => $user['user_type'] == 2 ? $meeting['person_price'] : $meeting['unit_price'],
            'create_time' => time(),
            'type' => 1,
            'pay_status' => 0,
            'uid' => $user['id'],
        ];
        if ($order_info['price'] == 0) {
            $order_info['pay_status'] = 1;
            $order_info['pay_time'] = time();
        }
        $id = $orderModel->save($order_info);

        if ($id) {
            $insert = ['mid' => $mid, 'uid' => $user['id'], 'create_time' => time(), 'status' => 0, 'order_id' => $orderModel->id];

            if ($order_info['price'] == 0) {

                $insert['status'] = 1;
            }
//            $insert = ['mid' => $mid, 'uid' => $user['id'], 'create_time' => time(), 'status' => 0, 'order_id' => $id];

            $logModel = new MeetingLogModel();
            $result = $logModel->save($insert);
            if ($result) {

                return $this->success('报名成功', '', ['log' => $result, 'order_id' => $orderModel->id, 'need_pay' => $order_info['price'] == 0 ? 0 : 1]);
            }
        } else {
            return $this->error('下单失败');

        }

    }
}